<?php
/**
 * Lookup Model.
 * @package    sitrack 
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright (c) 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Lookup Model.
 *
 * @package    sitrack 
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright (c) 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */
final class Lookup extends SPF_Model {

   /**
    * Constructor.
    *
    * @return  object
    */
   public function __construct( $table_name ) {

      /* construct the parent object */
      parent::__construct($table_name);
      
      $this->add_validation('description', true, 'custom');
      
   } // __construct

   /**
    * Destructor.
    *
    * @return  void
    */
   public function __destruct() {
      parent::__destruct();
   }
   
   /**
    * Override the clear() method to ensure the active field is defaulted to 1.
    *
    * @param   boolean   $keep_id   if true the primary key fields will not be cleared.
    * @return  void
    */
   public function clear( $keep_id = false ) {
       
       parent::clear($keep_id);
       
       $this->active = 1;
       
   } // clear
   
   /**
    * Returns an array of data from the specified table.
    *
    * @param   string    $table_name   name of the table to return the data form.    
    * @return  array
    */
   public static function browse_data( $table_name ) {
   
		if( SPF::$db->has_field($table_name, 'system_type') ) {
			$sql = "SELECT id,
                        description,
                        active,
                        system_type
                   FROM {$table_name}
               ORDER BY system_type,
                        description";
		}
		else {
      	$sql = "SELECT id,
                        description,
                        active
                   FROM {$table_name}
               ORDER BY description";
      }

      return SPF::$db->get_all($sql);
      
   } // browse_data
   
} // Lookup

?>
